import {spaceResourcesQuery} from 'api/space';
import {saveFeature, deleteFeature, updateFeature, queryLayerByKeys, saveNewLayer} from 'api/edit';

export default {
  namespace: 'edit',
  state: {
    layers: [],
    results: [],
    objectId: null
  },

  effects: {
    *getLayers({payload}, {call, put}) {
      const data = yield call(spaceResourcesQuery, payload);
      //url: netUrl + url
      if (data && data.success && data.data && data.data.length > 0) {
        let rlt = data.data;
        let lays = rlt.map((r) => {
          let ls = r.childrens.map((lay) => {
            let {appMapId, mapName, mapType, netUrl, url, tableId} = lay;
            return {
              id: appMapId,
              name: mapName,
              type: (mapType === 'layer_point') ? 0 : (mapType === 'layer_line') ? 1 : 2,
              url: netUrl + url,
              tableId: tableId
            }
          });
          return {title: r.mapName, list: ls};
        });
        yield put({
          type: 'setState',
          payload: {
            layers: lays
          },
        })
      } else {
        throw data
      }
    },
    * addFeature({payload}, {call, put}) {
      const data = yield call(saveFeature, payload);
      let {addResults} = data;
      let oid = '';
      if (addResults && addResults.length) {
        let {success, error, objectId} = addResults[0];
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              objectId: objectId
            }
          });
          oid = objectId;
        } else {
          alert(error.description + ' (' + error.code + ')');
        }
        return oid;
      }
    },
    * delFeature({payload}, {call, put}) {
      const data = yield call(deleteFeature, payload);
      let {deleteResults} = data;
      let res = false;
      if (deleteResults && deleteResults.length) {
        let {success, error, objectId} = deleteResults[0];
        if (success) {
          res = success;
          yield put({
            type: 'setState',
            payload: {
              objectId: ''
            }
          });
        }
      }
      return res;
    },
    * editFeature({payload}, {call, put}) {
      const data = yield call(updateFeature, payload);
      let {updateResults} = data;
      let oid = '';
      if (updateResults && updateResults.length) {
        let {success, error, objectId} = updateResults[0];
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              objectId: objectId
            }
          });
          oid = objectId;
        } else {
          alert(error.description + ' (' + error.code + ')');
        }
        return oid;
      }
    },
    *queryLayer({payload}, {call, put}){
      const {type} = payload;
      const data = yield call(queryLayerByKeys, payload);
      const list = data.features.map(item => {
        const {objectid, name, description} = item.attributes;
        return {id: objectid, name: name, type: type, description: description, imgSrc: ''};
      });
      yield put({
        type: 'setState',
        payload: {
          results: list
        },
      })
      return list;
    },
    //新增图层弹出编辑属性框
    *saveNewLayer({payload}, {call, put}) {
      const data = yield call(saveNewLayer, payload)
      return data
    },
  },

  reducers: {
    setState(state, action) {
      return {
        ...state,
        ...action.payload
      };
    }
  }
}
